const Ctoaes = require('./ctoaes');
// const PWDREPO = "cto";
const DB_HASH = "evlon-db-cdndb-p";
const DB_HASH_IV = "987123654456abcd";

module.exports = async (request) => {
    let url = new URL(request.url);
    let pathname = url.pathname;
    let pathitems = pathname.split('/');
    let token = pathitems[2];
    console.log(token);
    let aes = new Ctoaes();
    let pwdJson = aes.decryption_hex2utf8(token, Buffer.from(DB_HASH, "utf8"), Buffer.from(DB_HASH_IV, "utf8"));
    let pwdObj = JSON.parse(pwdJson);
    const init = {
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "GET, PUT, PATCH, POST, DELETE",
            "Access-Control-Allow-Headers": "*",
            'content-type': 'application/octet-stream'
        },
    }
    const body = pwdObj.p.substr(0,16);
    return new Response(body, init)
};